﻿using System;
using System.Windows;
using Microsoft.Phone.Controls;
using eLearning.Util;

namespace eLearning.View
{
    public partial class ConfigPage : PhoneApplicationPage
    {
        public ConfigPage()
        {
            InitializeComponent();
            textBoxServer.Text = Setting.Instance.LoadSetting(Config.ROOT_URL_KEY);
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            if (!ValidateServerUrl())
            {
                MessageBox.Show("Server address not valid!");
                return;
            }

            bool result = Setting.Instance.SaveSetting(Config.ROOT_URL_KEY, textBoxServer.Text);
            if (result)
            {
                MessageBox.Show("Configuration saved.");
                NavigationService.GoBack();
            }
        }


        private void CancelButton_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

        private bool ValidateServerUrl()
        {
            if (string.IsNullOrEmpty(textBoxServer.Text))
            {
                return false;
            }
            return true;
        }
    }
}